package com.sitech.ibnms.common.action;

import com.sitech.ibnms.common.form.TreeNode;
import com.sitech.ibnms.common.service.IAsyncTreeService;
import com.sitech.ibnms.core.BaseIbnmsAction;
import com.sitech.ibnms.util.AppException;
import com.sitech.ibnms.util.JSONUtil;

import java.util.Iterator;
import java.util.List;

/**
 * 异步树公用Action
 *
 * @author liujhc
 */
public class AsyncTreeAction extends BaseIbnmsAction {

	private IAsyncTreeService service;
	private List resultList;
	/**
	 * 根节点id,用于对KBP树进行局部展示
	 */
	private String rootId;
	/**
	 * 当前应该打开的节点id， 表示应该打开的节点
	 */
	private String id;
	/**
	 * 被点击的节点对象
	 */
	private TreeNode node;
	/**
	 * 用于查询子节点的Action名称，通过spring注入
	 */
	private String subNodesJsonAction;

    private String viewType;
	/**
	 * 展现kbp树页面
	 *
	 * @return
	 */
	@Override
	public String execute() throws AppException {
		try {
			TreeNode root = service.getRootNode(this.getCurrentUser().getLOGIN_ID(),rootId);
			if (root != null) {
				rootId = "[" + JSONUtil.toJSON(root) + "]";
			} else {
				rootId = JSONUtil.toJSON(service.getSubNodes(this.getCurrentUser().getLOGIN_ID(),this.getNode()));
			}
		} catch (NullPointerException e) {
			handleException(new AppException("无权限!", e));
		} catch (Exception e) {
			handleException(new AppException("展现树异常!", e));
		}
		log.debug(rootId);
		return SUCCESS;
	}

	/**
	 * 查询kbp某节点的子节点，返回json
	 *
	 * @return
	 */
	public String getSubNodesJson() throws AppException {
		try {
			resultList = service.getSubNodes(this.getCurrentUser().getLOGIN_ID(),this.getNode());
            Iterator<TreeNode> iter = resultList.iterator();
            while(iter.hasNext()){
                TreeNode node = iter.next();
                if("0".equals(node.getUserData("ENABLE"))){
                    iter.remove();
                }
            }
            log.debug(resultList);
		} catch (Exception e) {
			handleException(new AppException("获取树子节点信息异常!", e));
		}
		return SUCCESS;
	}

	public void setRootId(String rootId) {
		this.rootId = rootId;
	}

	public String getRootId() {
		return rootId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getSubNodesJsonAction() {
		return subNodesJsonAction;
	}

	public void setSubNodesJsonAction(String subNodesJsonAction) {
		this.subNodesJsonAction = subNodesJsonAction;
	}

	public TreeNode getNode() {
		if (node == null) {
			node = new TreeNode();
		}
		return node;
	}

	public List getResultList() {
		return resultList;
	}

	public void setService(IAsyncTreeService service) {
		this.service = service;
	}

    public String getViewType() {
        return viewType;
    }

    public void setViewType(String viewType) {
        this.viewType = viewType;
    }

    public static void main(String[] args) {
        String a = "11-14-12-11";
        System.out.println(a.substring(0, 11));;
    }
}
